package com.link.base.base.urlinfo.controller;

import javax.annotation.Resource;

import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;
import com.link.core.util.StringUtils;
import com.link.base.base.urlinfo.model.AppUrlDutyLink;
import com.link.base.base.urlinfo.service.AppUrlDutyLinkService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * app职责与URL-app职责与URL关联关系
 *
 * @author 郭闪
 * @date 2019/08/23 11:28
 */
@Controller
@RequestMapping("/link/appUrlDutyLink")
public class AppUrlDutyLinkController extends BasicController<AppUrlDutyLink> {


    @Resource
    private AppUrlDutyLinkService appUrlDutyLinkService;

    @Override
    public BasicService<AppUrlDutyLink> getBasicService() throws Exception {
        return appUrlDutyLinkService;
    }

    /**
     * 根据传入url字符串批量关联
     *
     * @author cuizhiyong
     * @param urls   多个待关联url组成的字符串
     * @param dutyId 职责id
     * @date 2019/4/9 20:48
     */
    @ResponseBody
    @PostMapping("/batchAddUrl")
    public Map<String, Object> batchAddUrl(String urls, Long dutyId) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (StringUtils.isBlank(urls)) {
                throw new ServiceException("URIINFO-001");
            }
            if (dutyId == null) {
                throw new ServiceException("URIINFO-002");
            }
            List<String> urlList = Arrays.stream(urls.split("\n"))
                    .filter(item -> !"".equals(item)).collect(Collectors.toList());
            appUrlDutyLinkService.batchAddUrl(urlList, dutyId);
            result.put("success", true);
        } catch (ServiceException bse) {
            result.put("success", false);
            result.put("result", bse.getMessage());
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            LCLogger.error().withMessage(e.getMessage()).flush();
        }
        return result;
    }

}
